<?php

defined('_JEXEC') or die('Restricted access');
use Joomla\Registry\Registry;
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

class ConsoleModelcomputingevent extends JModelList
{

    protected function getListQuery()
    {
        $app = JFactory::getApplication('site');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $params = $app->getParams();
        $this->setState('params', $params);
        $query->select(
            'ec.*,app.name'
        )
            ->from('pp_event_cfg as ec')
            ->join('INNER', 'pp_app AS app ON app.id = ec.app_id')
            ->where('app.account_id='.$user->id);
        $query->order('ec.id DESC');

        return $query;
    }

    public function getEvent()
    {
        $user = JFactory::getUser();
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select(
            'ec.*,app.name'
        )
            ->from('pp_event_cfg as ec')
            ->join('INNER', 'pp_app AS app ON app.id = ec.app_id')
            ->where('ec.comp_param_type=2 and app.account_id='.$user->id );
        $session = JFactory::getSession();
        $appId = $session->get('appId', -1);
        if ($appId > -1) {
        	$query->where('ec.app_id=' . $appId);
        }        
        $db->setQuery($query);
        $items = $this->_getList($query);
        if (count($items) > 0) {
	        foreach ($items as $key => $item) {
	            $itemArray[$item->id] = $item;
	        }
        }
        else {
        	$itemArray = array();
        }
        return $itemArray;
    }

    public function getItem($id = null)
    {

        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('event_cfg ')
            ->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }




}
